﻿<%@ WebHandler Language="C#" Class="YPContaienrTemperatureHandler" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Linq;
using BusinessRule.Business;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using System.Collections;
using BusinessRule.DTO;
using System.Web.SessionState;

//Description:批次温度维护信息处理程序
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:zhangrj
//create Date:2022-01-04
public class YPContaienrTemperatureHandler : IHttpHandler, IRequiresSessionState
{

    YPContainerBusiness ypContainerBusiness = new YPContainerBusiness();
    CurrentUserInfo m_UserInfo;
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request.Form["action"].ToString();
            switch (action)
            {
                case "GetContainerList":
                    GetContainerList(context);
                    break;
                case "GetContainerListBySearch":
                    GetContainerListBySearch(context);
                    break;
                case "GetSpecList":
                    GetWorkFlowStepList(context);
                    break;
                case "GetStepTemInfo":
                    GetStepTemperatureList(context);
                    break;
                case "Save":
                    SaveData(context);
                    break;
                case "Clear":
                    ClearData(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception e)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "出错了：" + e.Message, Result = false }));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    /// <summary>
    /// 查询批次列表
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerList(HttpContext context)
    {
        Dictionary<string, object> para = new Dictionary<string, object>();
        para.Add("ContainerId", context.Request.Form["containerId"].ToString());
        para.Add("WorkflowId", context.Request.Form["workflowId"].ToString());
        DataTable dt = ypContainerBusiness.GetContainerTemperatureInfo(para);
        uMESProcessResult res = new uMESProcessResult() { RowCount = dt.Rows.Count.ToString(), DBTable = dt };
        context.Response.Write(JsonConvert.SerializeObject(res));
    }

    /// <summary>
    /// 查询批次列表
    /// add:Wangjh 20211224
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerListBySearch(HttpContext context)
    {
        m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        Dictionary<string, object> para = new Dictionary<string, object>();
        para.Add("MfgorderName", context.Request.Form["mfgorderName"].ToString());
        para.Add("ContainerName", context.Request.Form["containerName"].ToString());
        para.Add("FactoryID", m_UserInfo.FactoryID);
        para.Add("Status", context.Request.Form["status"].ToString());//温度状态
        DataTable dt = ypContainerBusiness.GetContainerTemperatureInfoBySearch(para);
        uMESProcessResult res = new uMESProcessResult() { RowCount = dt.Rows.Count.ToString(), DBTable = dt };
        context.Response.Write(JsonConvert.SerializeObject(res));
    }

    /// <summary>
    /// 通过工艺ID获取工序信息列表
    /// </summary>
    /// <param name="context"></param>
    public void GetWorkFlowStepList(HttpContext context)
    {
        string workFlowId = context.Request.Form["workFlowId"].ToString();
        DataTable dt = ypContainerBusiness.GetWorkflowStepForTem(workFlowId);
        //获取批次所有工序温度信息
        string strTem = "";
        string containerId = context.Request.Form["containerId"].ToString();
        foreach (DataRow item in dt.Rows)
        {
            DataTable dt1 = ypContainerBusiness.GetStepTemperatureInfo(containerId, item["SpecNo"].ToString());
            if (dt1.Rows.Count > 0)
            {
                strTem += item["SpecName"].ToString() + "：";
                foreach (DataRow dr in dt1.Rows)
                {
                    if (dr["TEMPERATURENAME"].ToString() == "温度审签中" || dr["TEMPERATURENAME"].ToString() == "温度不维护")
                    {
                        strTem += dr["TEMPERATURENAME"].ToString() + "；";
                    }
                    else
                    {
                        strTem += dr["TEMPERATURENAME"].ToString() + "：" + dr["TEMPERATUREVALUE"].ToString() + "；";
                    }
                }
                strTem = strTem.Trim('；') + "。";
            }
        }
        uMESProcessResult res = new uMESProcessResult() { RowCount = dt.Rows.Count.ToString(), DBTable = dt, ReturnData = strTem };
        context.Response.Write(JsonConvert.SerializeObject(res));
    }

    /// <summary>
    /// 获取工序温度列表
    /// </summary
    /// 
    /// <param name="context"></param>
    public void GetStepTemperatureList(HttpContext context)
    {
        string containerId = context.Request.Form["containerId"].ToString();
        string specNo = context.Request.Form["specNo"].ToString();
        DataTable dt = ypContainerBusiness.GetStepTemperatureInfo(containerId, specNo);
        foreach (DataRow item in dt.Rows)
        {
            item["TEMPERATUREVALUE"] = item["TEMPERATUREVALUE"].ToString().Trim('℃');
        }
        uMESProcessResult res = new uMESProcessResult() { DBTable = dt };
        context.Response.Write(JsonConvert.SerializeObject(res));
    }

    /// <summary>
    /// 保存勾选的数据
    /// </summary>
    /// <param name="context"></param>
    public void SaveData(HttpContext context)
    {
        CurrentUserInfo m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        List<TemStepModel> temsModel = JsonConvert.DeserializeObject<List<TemStepModel>>(context.Request.Form["stepData"].ToString());
        List<TemModel> temModel = JsonConvert.DeserializeObject<List<TemModel>>(context.Request.Form["temData"].ToString());
        if (temModel == null || temModel.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "未勾选或填写温度", Result = false }));
            return;
        }
        if (temsModel == null || temsModel.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "未选择工序", Result = false }));
            return;
        }
        List<Dictionary<string, string>> dicList = new List<Dictionary<string, string>>();
        foreach (TemModel tem in temModel)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("TemName", tem.TemName);
            dic.Add("TemValue", tem.TemValue);
            dicList.Add(dic);
        }
        foreach (TemStepModel item in temsModel)
        {
            ypContainerBusiness.SaveStepTemperatureData(item.ContainerId, item.SpecNo, item.SpecDesc, m_UserInfo.EmployeeID.ToString(), dicList);
        }
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "保存成功", Result = true }));
    }
    /// <summary>
    /// 清除勾选的工序温度
    /// </summary>
    /// <param name="context"></param>
    public void ClearData(HttpContext context)
    {
        List<TemStepModel> temsModel = JsonConvert.DeserializeObject<List<TemStepModel>>(context.Request.Form["stepData"].ToString());
        if (temsModel == null || temsModel.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "未选择工序", Result = false }));
            return;
        }
        foreach (TemStepModel item in temsModel)
        {
            ypContainerBusiness.SaveStepTemperatureData(item.ContainerId, item.SpecNo, item.SpecDesc, "", null);//删除该工序所有温度数据
        }
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "清空成功", Result = true }));
    }

    public class TemStepModel
    {
        public string ContainerId { get; set; }
        public string SpecNo { get; set; }
        public string SpecDesc { get; set; }
    }

    public class TemModel
    {
        public string TemName { get; set; }
        public string TemValue { get; set; }
    }

}